--if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SelectDataWithPaging]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
--drop procedure [dbo].[Paging_RowCount]
--GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON  
GO

ALTER PROCEDURE SelectDataWithPaging
(
@Tables nvarchar(1000),
@PK nvarchar(100),
@Sort nvarchar(200) = NULL,
@PageNumber int = 1,
@PageSize int = 10,
@Fields nvarchar(1000) = '*',
@Filter nvarchar(1000) = NULL,
@Group nvarchar(1000) = NULL)
AS
/*Default Sorting*/
IF @Sort IS NULL OR @Sort = ''
	SET @Sort = @PK
/*Find the @PK type*/
DECLARE @SortTable nvarchar(100)
DECLARE @SortName nvarchar(100)
DECLARE @strSortColumn nvarchar(200)
DECLARE @operator char(2)
DECLARE @type nvarchar(100)
DECLARE @prec int
/*Set sorting variables.*/
IF CHARINDEX('DESC',@Sort)>0
	BEGIN
		SET @strSortColumn = REPLACE(@Sort, 'DESC', '')
		SET @operator = '<='
	END
ELSE
	BEGIN
		IF CHARINDEX('ASC', @Sort) > 0
			SET @strSortColumn = REPLACE(@Sort, 'ASC', '')
		SET @operator = '>='
	END
IF CHARINDEX('.', @strSortColumn) > 0
	BEGIN
		SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))
		SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1, LEN(@strSortColumn))
	END
ELSE
	BEGIN
		SET @SortTable = @Tables
		SET @SortName =RTRIM( LTRIM(@strSortColumn) )
	END
SELECT @type=t.name, @prec=c.prec
FROM sysobjects o 
JOIN syscolumns c on o.id=c.id
JOIN systypes t on c.xusertype=t.xusertype
WHERE o.name = @SortTable AND c.name = @SortName


IF CHARINDEX('char', @type) > 0
   SET @type = @type + '(' + CAST(@prec AS nvarchar) + ')'
SET @SortTable = '[' + @SortTable + ']'
SET @SortName = '[' + @SortName + ']'
SET @Tables = '[' + @Tables + ']'
	
DECLARE @strPageSize nvarchar(50)
DECLARE @strStartRow nvarchar(50)
DECLARE @strFilter nvarchar(1000)
DECLARE @strSimpleFilter nvarchar(1000)
DECLARE @strGroup nvarchar(1000)
/*Default Page Number*/
IF @PageNumber < 1
	SET @PageNumber = 1
/*Set paging variables.*/
SET @strPageSize = CAST(@PageSize AS nvarchar(50))
SET @strStartRow = CAST(((@PageNumber - 1)*@PageSize + 1) AS nvarchar(50))
/*Set filter & group variables.*/
IF @Filter IS NOT NULL AND @Filter != ''
	BEGIN
		SET @strFilter = ' WHERE ' + @Filter + ' '
		SET @strSimpleFilter = ' AND ' + @Filter + ' '
	END
ELSE
	BEGIN
		SET @strSimpleFilter = ''
		SET @strFilter = ''
	END
IF @Group IS NOT NULL AND @Group != ''
	SET @strGroup = ' GROUP BY ' + @Group + ' '
ELSE
	SET @strGroup = ''
/*Execute dynamic query*/

EXEC (
'DECLARE @SortColumn ' + @type + '
SET ROWCOUNT ' + @strStartRow + '
SELECT @SortColumn=' + @strSortColumn + ' FROM ' + @Tables + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + '
SET ROWCOUNT ' + @strPageSize + '
SELECT ' + @Fields + ' FROM ' + @Tables + ' WHERE ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + '
')

 EXEC ('SELECT COUNT(*) FROM ' + @Tables + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + ')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO